package com.ktjy.controller;


import com.ktjy.dto.WorkFlowDTO;
import com.ktjy.pojo.TbActivity;
import com.ktjy.service.IWorkFlowService;
import com.ktjy.utils.AjaxResult;
import com.ktjy.utils.PageUtil;
import com.ktjy.vo.ActCommentEntity;
import com.ktjy.websocket.WebSocketServer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/workFlow")
@Tag(name="用户模块")
@Slf4j
public class WorkFlowController {
    @Autowired
    IWorkFlowService workFlowService;
    @Autowired
    WebSocketServer webSocketServer;

    @Operation(summary="启动流程")
    @PutMapping("/startProcess/{id}")
    public AjaxResult startProcess(@PathVariable("id")Integer id) {
        try {
            String processDefinitionKey="Activity3";
            workFlowService.startProcess(processDefinitionKey,id);

            webSocketServer.send("收到一个待办任务，请及时处理");

            return AjaxResult.success("流程启动成功");
        } catch (Exception e) {
            log.error("WorkFlowController中startProcess方法执行报错的。",e);
        }
        return AjaxResult.error("流程启动失败");
    }

    @Operation(summary="查询当前登录用户的待办任务")
    @GetMapping("/loadTask")
    public AjaxResult loadTask(WorkFlowDTO workFlowDTO,
                               @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                               @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        try {
            PageUtil page = workFlowService.findTask(workFlowDTO, pageNo, pageSize);
            return AjaxResult.success(page);
        } catch (Exception e) {
            log.error("WorkFlowController中loadTask方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @Operation(summary="根据任务ID查询业务数据")
    @GetMapping("/loadTask/{taskId}")
    public AjaxResult loadTask(@PathVariable("taskId") String taskId) {
        try {
            TbActivity activity = workFlowService.findActivityByTaskId(taskId);
            return AjaxResult.success(activity);
        } catch (Exception e) {
            log.error("WorkFlowController中loadTask方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @Operation(summary="根据任务ID查询批注信息")
    @GetMapping("/loadComment/{taskId}")
    public AjaxResult loadComment(@PathVariable("taskId")String taskId) {
        try {
            List<ActCommentEntity> comments = workFlowService.findCommentByTaskId(taskId);
            return AjaxResult.success(comments);
        } catch (Exception e) {
            log.error("WorkFlowController中loadComment方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @Operation(summary="完成任务")
    @PutMapping("/completeTask")
    public AjaxResult completeTask(@RequestBody WorkFlowDTO workFlowDTO) {
        try {
            workFlowService.completeTask(workFlowDTO);
            return AjaxResult.success("任务完成成功!");
        } catch (Exception e) {
            log.error("WorkFlowController中completeTask方法执行报错的。",e);
        }
        return AjaxResult.error("任务完成失败!");
    }
}
